package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.dto.admin.task.TaskWithExamPaperDTO;
import com.mindskip.xzs.domain.vo.admin.task.TaskWithExamPaperVO;
import com.mindskip.xzs.domain.vo.admin.task.TaskVO;
import com.mindskip.xzs.domain.dto.admin.task.TaskPageDTO;
import com.mindskip.xzs.service.TaskExamService;
import com.mindskip.xzs.utility.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("AdminTaskController")
@RequestMapping(value = "/api/admin/task")
public class TaskController {

    private final TaskExamService taskExamService;

    public TaskController(TaskExamService taskExamService) {
        this.taskExamService = taskExamService;
    }

    @PostMapping("/page")
    public RestResponse<PageInfo<TaskVO>> page(@RequestBody TaskPageDTO taskPageDTO) {

        System.out.println(taskPageDTO);
        PageInfo<TaskVO> pageInfo = taskExamService.getTaskExamByPage(taskPageDTO);
        return RestResponse.ok(pageInfo);
    }

    @PostMapping("/select/{id}")
    public RestResponse<TaskWithExamPaperVO> select(@PathVariable Integer id) {

        TaskWithExamPaperVO taskWithExamPaperVO = taskExamService.getTaskWithExamPaperVO(id);

        return RestResponse.ok(taskWithExamPaperVO);
    }

    @PostMapping("/edit")
    public RestResponse<TaskWithExamPaperVO> edit(@RequestBody @Valid TaskWithExamPaperDTO taskWithExamPaperDTO) {

        taskExamService.insertOrUpdate(taskWithExamPaperDTO);

        TaskWithExamPaperVO taskWithExamPaperVO = taskExamService.getTaskWithExamPaperVO(taskWithExamPaperDTO.getId());
        return RestResponse.ok(taskWithExamPaperVO);
    }

    @PostMapping("/delete/{id}")
    public RestResponse<Object> delete(@PathVariable Integer id) {

        taskExamService.softDelete(id);
        return RestResponse.ok();
    }
}
